package com.sbs.gaoxinqutoilet.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;

import com.just.agentweb.AgentWeb;
import com.sbs.gaoxinqutoilet.R;
import com.sbs.gaoxinqutoilet.base.BaseBackFragment;
import com.sbs.gaoxinqutoilet.http.HttpApis;
import com.sbs.gaoxinqutoilet.interfaces.Constants;
import com.sbs.gaoxinqutoilet.utils.SPUtil;
import com.zhy.autolayout.AutoRelativeLayout;
import com.zhy.http.okhttp.OkHttpUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by 聪明一只哈 on 2020/4/26.
 * Time: 2020/4/26  10:08
 */
public class NewsContentFragment extends BaseBackFragment {

    public static String NEWS_Key = "new_content_id";

    Unbinder unbinder;
    @BindView(R.id.ll_webview_content)
    LinearLayout llWebviewContent;
    @BindView(R.id.toolbar_back)
    ImageView toolbarBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar_iv_add)
    ImageView toolbarIvAdd;
    @BindView(R.id.toolbar_iv_menu)
    ImageView toolbarIvMenu;
    @BindView(R.id.toolbar_tv_menu)
    TextView toolbarTvMenu;
    @BindView(R.id.toolbar_rl_menu)
    AutoRelativeLayout toolbarRlMenu;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    /* @BindView(R.id.webview_news)
     WebView webviewNews;*/
    private AgentWeb agentWeb;
    private String newsID;

    public static NewsContentFragment newInstance(String newsId) {
        Bundle args = new Bundle();
        args.putSerializable(NEWS_Key, newsId);
        NewsContentFragment fragment = new NewsContentFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_newscontent, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView(inflater);
        initData();
        initListener();
        return attachToSwipeBack(view);
    }




    private void initView(LayoutInflater inflater) {

        //设置回键
        initToolbarBack(toolbar);
        toolbarTitle.setText("资讯");
        //返回按钮
        toolbarBack.setVisibility(View.VISIBLE);


        //获取传递过来的值
        Bundle arguments = getArguments();
        if (arguments != null) {
            newsID = arguments.getString(NEWS_Key);
        }
        String token = SPUtil.getString(Constants.KEY_TOKEN, "");
        if (!TextUtils.isEmpty(token) && !TextUtils.isEmpty(newsID)) {//"1410017d49bb-fb20-45af-82c2-e7656965927e"
            String url = HttpApis.newContent_detial + newsID + "?token=" + token;
            agentWeb = AgentWeb.with(this)
                    .setAgentWebParent((LinearLayout) llWebviewContent, new LinearLayout.LayoutParams(-1, -1))
                    .useDefaultIndicator()
                    .createAgentWeb()
                    .ready()
                    .go(url);
        }

//        String url = HttpApis.newContent_detial+"1410017d49bb-fb20-45af-82c2-e7656965927e"+"?token=" + token;
//        agentWeb = AgentWeb.with(this)
//                .setAgentWebParent((LinearLayout) llWebviewContent, new LinearLayout.LayoutParams(-1, -1))
//                .useDefaultIndicator()
//                .createAgentWeb()
//                .ready()
//                .go(url);


    }

    private void initData() {

    }

    private void initListener() {

    }


    @Override
    public boolean onBackPressedSupport() {


        WebView webView = agentWeb.getWebCreator().getWebView();
        if (webView.canGoBack()) {
            webView.goBack();
            return true;
        } else {
            return super.onBackPressedSupport();
        }


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

        unbinder.unbind();
        //EventBus.getDefault().unregister(this);
        OkHttpUtils.getInstance().cancelTag(this);
        cancelLoadingDialog();

    }
}
